//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.yuebanmaoshare.file.common.operation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import net.sf.sevenzipjbinding.ExtractAskMode;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IArchiveExtractCallback;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.PropID;
import net.sf.sevenzipjbinding.SevenZipException;

public class ExtractCallback implements IArchiveExtractCallback {
    private int index;
    private IInArchive inArchive;
    private String ourDir;

    public ExtractCallback(IInArchive inArchive, String ourDir) {
        this.inArchive = inArchive;
        this.ourDir = ourDir;
    }

    public void setCompleted(long arg0) throws SevenZipException {
    }

    public void setTotal(long arg0) throws SevenZipException {
    }

    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        this.index = index;
        String path = (String)this.inArchive.getProperty(index, PropID.PATH);
        boolean isFolder = (Boolean)this.inArchive.getProperty(index, PropID.IS_FOLDER);
        return (data) -> {
            try {
                if (!isFolder) {
                    File file = new File(this.ourDir + File.separator + path);
                    save2File(file, data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return data.length;
        };
    }

    public void prepareOperation(ExtractAskMode arg0) throws SevenZipException {
    }

    public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
    }

    public static boolean save2File(File file, byte[] msg) {
        OutputStream fos = null;

        boolean var21;
        try {
            File parent = file.getParentFile();
            if (parent.exists() || parent.mkdirs()) {
                fos = new FileOutputStream(file, true);
                fos.write(msg);
                fos.flush();
                var21 = true;
                return var21;
            }

            var21 = false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            var21 = false;
            return var21;
        } catch (IOException e) {
            e.printStackTrace();
            var21 = false;
            return var21;
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return var21;
    }
}
